package com.kevin.Code.String;

/**
 * @author Vinlee Xiao
 * @Classname CompareVersionNumbers
 * @Description Leetcode 165. 比较版本号 字符串 中等难度
 * @Date 2021/12/1 10:23
 * @Version 1.0
 */
public class Leetcode165_CompareVersionNumbers {

    /**
     * @param version1
     * @param version2
     * @return
     */
    public int compareVersion(String version1, String version2) {


        String[] split1 = version1.split("\\.");
        String[] split2 = version2.split("\\.");

        int len1 = split1.length;
        int len2 = split2.length;

        int index = 0;

        while (index < len1 && index < len2) {

            int num1 = Integer.parseInt(split1[index]);
            int num2 = Integer.parseInt(split2[index]);

            if (num1 > num2) {
                return 1;
            } else if (num1 < num2) {
                return -1;
            }

            //索引坐标增加
            index++;

        }

        //如果version1 还有数字 version2没有版本号了
        if (index < len1) {
            int num1 = Integer.parseInt(split1[index]);
            if (num1 > 0) {
                return 1;
            }
            index++;
        }

        //version2还有版本号
        if (index < len2) {
            int num2 = Integer.parseInt(split2[index]);
            if (num2 > 0) {
                return -1;
            }
            index++;
        }
        return 0;
    }

    public static void main(String[] args) {
        Leetcode165_CompareVersionNumbers leetcode165_compareVersionNumbers = new Leetcode165_CompareVersionNumbers();
        int i = leetcode165_compareVersionNumbers.compareVersion("1.0.1", "1");
        System.out.println(i);
    }
}
